<div class="layui-fluid" id="VIEW-list-table" lay-title="客户池">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">筛选数据</div>
                <div class="layui-card-body">
                    <form class="layui-form" method="get" lay-filter="lig-customer-searchform">
                        <input type="hidden" name="status" value="0">
                        <div class="layui-form-item" style="margin-bottom:0;">
                            <div class="layui-inline">
                                <label class="layui-form-label">公司</label>
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" name="name" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">法人</label>
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" name="legal" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">行业</label>
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" name="industry" />
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item" style="margin-bottom:0;">
                            <div class="layui-inline">
                                <label class="layui-form-label">区域</label>
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" name="area" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">时间标签</label>
                                <div class="layui-input-inline">
                                    <select name="timeTag">
                                        <option value="">--不限--</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">起止日期</label>
                                <div class="layui-inline">
                                    <input type="text" class="layui-input" autocomplete="off" name="startTime" />
                                </div>
                                <div class="layui-inline">--</div>
                                <div class="layui-inline">
                                    <input type="text" name="endTime" autocomplete="off" class="layui-input">
                                </div>
                                
                            </div>
                            
                        </div>
                        <div class="layui-form-item" style="margin-bottom:0;">
                            <div class="layui-inline">
                                <label class="layui-form-label">手机号</label>
                                <div class="layui-input-inline">
                                    <input type="text" class="layui-input" name="telNumber" />
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">业务员</label>
                                <div class="layui-input-inline">
                                    <select name="salesId">
                                        <option value="">--不限--</option>
                                        <option value="0">无业务员</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label" style="width:20px;"></label>
                                <input type="button" class="layui-btn layui-btn-sm" lay-filter="lig-customer-search" value="&nbsp;查&nbsp;询&nbsp;"></input>
                                    <input type="reset" class="layui-btn layui-btn-sm layui-btn-primary" value=" 重 置 ">
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" lay-filter="lig-customer-import">
                                        <i class="layui-icon layui-icon-export"></i>批量导入
                                    </button>
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" lay-filter="lig-customer-export">
                                        <i class="layui-icon layui-icon-export"></i> 导 出 
                                    </button>

                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-body nepadmin-table-full">
                    <div class="layui-tab layui-tab-brief" lay-filter="lig-customer-status-tab">
                        <ul class="layui-tab-title" id="customer_status">
                        </ul>
                    </div>
                    <div class="layui-btn-container nepadmin-pad-t10 nepadmin-pad-l10">
                        <div class="layui-btn layui-btn-sm" lay-filter="lig-customer-add">添加客户</div>
                        <div class="layui-btn layui-btn-sm layui-btn-primary" lay-filter="lig-customer-remove">删除</div>
                        <div class="layui-btn layui-btn-sm layui-btn-primary" lay-filter="lig-customer-toSales">分配业务员</div>
                        <div class="layui-btn layui-btn-sm layui-btn-primary" lay-filter="lig-customer-moveStatus">移动到</div>
<!--                         <div class="layui-btn layui-btn-sm layui-btn-primary" lay-filter="lig-customer-checkData">数据清洗</div>
 -->                    </div>
                    <table lay-filter="lig-customer-table"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="tpl-customer-table-row-toolBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="lig-detail" title="详情" lay-data="{{d.id}}">详情</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="lig-modify" title="编辑">编辑</a>
</script>
<script type="text/html" id="tpl-customer-add">
    <div class="layui-form" style="padding: 20px 20px 0 0;" lay-filter="lig-saveForm">
        <div class="layui-form-item">
            <label class="layui-form-label ligui-form-item-required">状态</label>
            <div class="layui-input-block">
                <select name="status" lay-verify="required">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label ligui-form-item-required">公司</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" class="layui-input"
                       placeholder="客户名称" value='' lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label ligui-form-item-required">法人</label>
                <div class="layui-input-block">
                    <input type="text" name="legal" autocomplete="off" class="layui-input"
                           value="" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">微信</label>
                <div class="layui-input-block">
                    <input type="radio" name="wxNumber" value="加" title="加">
                    <input type="radio" name="wxNumber" value="是" title="是">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-bottom:0px;">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <span style="height: 34px;line-height: 34px;">
                    <i class="layui-icon layui-icon-add-circle" lay-filter="lig-customer-tel-add" style="font-size: 24px; cursor: pointer;"></i>
                </span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <table class="layui-table" lay-skin="nob" lay-size="sm" style="border:0;">
                    <colgroup>
                        <col width="70">
                        <col width="100">
                        <col width="50">
                    </colgroup>
                    <tbody class="customer-tels">
                    <tr>
                        <td style="padding-left:0;">
                            <input type="text" name="telNumber" autocomplete="off" class="layui-input"
                                   value="" style="height:28px;line-height:28px;">
                        </td>
                        <td style="text-align:right;" class="col-opt">
                            <input type="hidden" name="isLegal" value="0"/>
                            <a href="javascript:;" lay-filter="lig-setLegalTel">设置为法人电话</a>
                        </td>
                        <td style="text-align:right;">
                            <input type="hidden" name="isInvalid" value="0"/>
                            <a href="javascript:;" lay-filter="lig-waitTel">待确</a>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" style="height:180px;line-height:20px;"></textarea>
            </div>
        </div>
    </div>
</script>
<script type="text/html" id="tpl-customer-modify">
    <div class="layui-form" style="padding: 20px 20px 0 0;" lay-filter="lig-saveForm">
        <input type="hidden" name="id" value="{{d.id}}"/>
        <div class="layui-form-item">
            <label class="layui-form-label ligui-form-item-required">状态</label>
            <div class="layui-input-block">
                <select name="status" lay-verify="required" lay-data="{{d.status}}">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label ligui-form-item-required">公司</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" class="layui-input"
                       value="{{d.name}}" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label ligui-form-item-required">法人</label>
                <div class="layui-input-inline">
                    <input type="text" name="legal" autocomplete="off" class="layui-input"
                           value="{{d.legal}}" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">微信</label>
                <div class="layui-input-inline">
                    <input type="radio" name="wxNumber" value="加" title="加" {{# if (d.wxNumber == '加') { }} checked {{# } }}>
                    <input type="radio" name="wxNumber" value="是" title="是" {{# if (d.wxNumber == '是') { }} checked {{# } }}>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-bottom:0px;">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <span style="height: 34px;line-height: 34px;">
                    <i class="layui-icon layui-icon-add-circle" lay-filter="lig-customer-tel-add" style="font-size: 24px; cursor: pointer;"></i>
                </span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <table class="layui-table" lay-skin="nob" lay-size="sm" style="border:0;">
                    <colgroup>
                        <col width="70">
                        <col width="100">
                        <col width="50">
                    </colgroup>
                    <tbody class="customer-tels">
                    {{# layui.each(d.tels, function(i, it){ }}
                    <tr>
                        <td style="padding-left:0;">
                            <input type="text" name="telNumber" autocomplete="off" class="layui-input"
                                   value="{{it.telNumber}}" style="height:28px;line-height:28px;">
                        </td>
                        <td style="text-align:right;" class="col-opt">
                            {{#
                            if (it.isLegal == 1) {
                            }}
                            <input type="hidden" name="isLegal" value="1"/>
                            <a href="javascript:;" class="layui-badge layui-bg-green" lay-filter="lig-cancelLegalTel" title="点击取消">
                                法人电话
                            </a>
                            {{# } else { }}
                            <input type="hidden" name="isLegal" value="0"/>
                            <a href="javascript:;" lay-filter="lig-setLegalTel">设置为法人电话</a>
                            {{# } }}
                        </td>
                        <td style="text-align:right;">
                            {{#
                            if (it.isInvalid == 1) {
                            }}
                            <input type="hidden" name="isInvalid" value="1"/>
                            <a href="javascript:;" class="layui-badge layui-bg-blue" lay-filter="lig-waitingTel" title="点击取消">待确</a>
                            {{# } else { }}
                            <input type="hidden" name="isInvalid" value="0"/>
                            <a href="javascript:;" lay-filter="lig-waitTel">待确</a>
                            {{# } }}
                        </td>
                    </tr>
                    {{#  }); }}
                    </tbody>
                </table>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">待跟进时间</label>
            <div class="layui-input-inline">
              <input type="text" name="followTime" autocomplete="off" class="layui-input {{ d.followTime || 'layui-disabled' }} " value="{{layui.lig.toDateString(d.followTime, 'yyyy-MM-dd') }}" {{ d.followTime || 'disabled' }}>
            </div>
            <div class="layui-form-mid layui-opt-aux" style="margin-left:6px;">
                <input type="checkbox" name="notFollow" lay-skin="primary" title="不跟进" {{ d.followTime || 'checked' }}  lay-filter="lig-notFollow">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" style="height:180px;line-height:20px;">{{d.remark}}</textarea>
            </div>
        </div>
    </div>
</script>
<script type="text/html" id="tpl-customer-detail">
    <span class="layui-layer-setwin"><a class="layui-layer-ico layui-layer-close layui-layer-close1" href="javascript:;"></a></span>
    <form class="layui-form" style="padding-top: 40px;" lay-filter="lig-saveForm">
        <div class="layui-collapse">
          <div class="layui-colla-item">
            <h2 class="layui-colla-title">基本信息</h2>
            <div class="layui-colla-content layui-show">
                <div class="layui-form-item" style="margin-bottom:0;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">公司：</label>
                        <div class="layui-input-inline" style="width:280px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{d.name}}</span>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-bottom:0;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">区域：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{d.area}}</span>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">行业：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{d.industry}}</span>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-bottom:0;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">法人：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{d.legal}}</span>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">微信：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{d.wxNumber}}</span>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-bottom:0;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">业务员：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{d.salesName}}</span>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">状态：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{d.statusName}}</span>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-bottom:0;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">更新时间：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{layui.util.toDateString(d.modifiedTime, "yyyy-MM-dd HH:mm:ss") }}</span>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">入库时间：</label>
                        <div class="layui-input-inline" style="width:140px;">
                            <span style="height: 30px; line-height: 30px;color: #999!important;">{{layui.util.toDateString(d.createdTime, "yyyy-MM-dd HH:mm:ss") }}</span>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-bottom:0;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:70px;">电话：</label>
                    </div>
                </div>
                <div class="layui-form-item" style="margin-bottom:0;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:50px;"></label>
                        <div class="layui-input-block">
                            <table class="layui-table" lay-skin="line" lay-size="sm" style="border:0;">
                                <colgroup>
                                    <col width="200">
                                    <col width="150">
                                </colgroup>
                                <tbody>
                                    {{# 
                                        layui.each(d.tels, function(i, it){ }}
                                      <tr lay-data="{{it.id}}">
                                        <td>{{ it.telNumber }}</td>
                                        <td style="text-align:right;" class="col-opt">
                                        {{#
                                            if (it.isLegal == 1) {
                                        }}
                                            <span class="layui-badge layui-bg-green">法人电话</span>
                                        {{# } }}
                                        </td>
                                      </tr>
                                    {{#  }); }}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
          </div>
          <div class="layui-colla-item">
            <h2 class="layui-colla-title">备注</h2>
              <div class="layui-colla-content layui-show" style="padding:0;">
                  <textarea name="remark" class="layui-textarea" disabled style="height:150px;line-height:20px;">{{d.remark}}</textarea>
              </div>
          </div>
          <div class="layui-colla-item">
            <h2 class="layui-colla-title">申贷记录</h2>
            <div class="layui-colla-content layui-show"></div>
          </div>
        </div>
        
    </form>
</script>
<script type="text/html" id="tpl-customer-tel">
    <tr>
        <td style="padding-left:0;">
            <input type="text" name="telNumber" autocomplete="off" class="layui-input" value=""
                   style="height:28px;line-height:28px;">
        </td>
        <td style="text-align:right;" class="col-opt">
            <input type="hidden" name="isLegal" value="0"/>
            <a href="javascript:;" lay-filter="lig-setLegalTel">设置为法人电话</a>
        </td>
        <td style="text-align:right;">
            <input type="hidden" name="isInvalid" value="0"/>
            <a href="javascript:;" lay-filter="lig-waitTel">待确</a>
        </td>
    </tr>
</script>
<script type="text/html" id="tpl-customer-moveStatus">
    <div class="layui-form" style="padding: 20px 20px 0 0;" lay-filter="lig-saveForm">
        <div class="layui-form-item">
            <label class="layui-form-label ligui-form-item-required">状态</label>
            <div class="layui-input-block">
                <select name="status" lay-verify="required">
                    <option value=""></option>
                </select>
            </div>
        </div>
    </div>
</script>
<script>
    var choose_user, salesQuantity_dataJson;
    layui.use(['laytpl', 'table', 'form', 'layer', 'element', 'laydate', 'jquery', 'admin', 'lig', 'selectN'], function (laytpl, table, form, layer, element, laydate, $, admin, lig, selectN) {
        
        

        var getSelected = function() {
            var _datas = table.checkStatus('customerId').data,  _ids = ''
                , _length = _datas.length;
            if (_length > 0) {
                $.each(_datas, function (i, item) {
                    _ids += "," + item["id"];
                });
                if (_ids != '') {
                    _ids = _ids.substring(1);
                }
            }
            return _ids;
        };


        var initStatus;
        var setLegalStatus = function() {
            var length = $('.customer-tels').find('[lay-filter="lig-cancelLegalTel"]').length, $status = $('[lay-filter="lig-saveForm"] select[name="status"]');
            if (length > 0) {
                form.val("lig-saveForm", {status: '01001'});
            } else {
                form.val("lig-saveForm", {status: initStatus});
            }
            form.render("select");
        };

        var renderSetLegalTel = function() {
            $('[lay-filter="lig-setLegalTel"]').unbind('click').click(function () {
                var $this = $(this), $td = $this.parent();
                //$this.closest('tbody').find('.col-opt').html('<input type="hidden" name="isLegal" value="0"/><a href="javascript:;" lay-filter="lig-setLegalTel">设置为法人电话</a>');
                $td.html('<input type="hidden" name="isLegal" value="1"/><a href="javascript:;" class="layui-badge layui-bg-green" lay-filter="lig-cancelLegalTel" title="点击取消">法人电话</a>');
                $td.next().html('<input type="hidden" name="isInvalid" value="0"/><a href="javascript:;" lay-filter="lig-waitTel">待确</a>');
                renderCancelLegalTel();
                renderWaitTel();

                setLegalStatus();
            });
        };

        var renderCancelLegalTel = function() {
            $('[lay-filter="lig-cancelLegalTel"]').unbind('click').click(function () {
                var $this = $(this), $td = $this.parent();
                $td.html('<input type="hidden" name="isLegal" value="0"/><a href="javascript:;" lay-filter="lig-setLegalTel">设置为法人电话</a>');
                renderSetLegalTel();

                setLegalStatus();
            });

        };

        var renderDelTel = function() {
            $('[lay-filter="lig-delTel"]').unbind('click').click(function () {
                $(this).closest('tr').remove();
            });
        };

        var renderWaitTel = function () {
            $('[lay-filter="lig-waitTel"]').unbind('click').click(function () {
                var $this = $(this), $td = $this.parent();
                $td.html('<input type="hidden" name="isInvalid" value="1"/><a href="javascript:;" class="layui-badge layui-bg-blue" lay-filter="lig-waitingTel" title="点击取消">待确</a>');
                $td.prev().html('<input type="hidden" name="isLegal" value="0"/><a href="javascript:;" lay-filter="lig-setLegalTel">设置为法人电话</a>');
                renderWaitingTel();
                renderSetLegalTel();
            });
        }

        var renderWaitingTel = function () {
            $('[lay-filter="lig-waitingTel"]').unbind('click').click(function () {
                var $this = $(this), $td = $this.parent();
                $td.html('<a href="javascript:;" lay-filter="lig-waitTel">待确</a>');
                renderWaitTel();
            });
        }

        var renderAddTel = function () {
            $('.customer-tels').append($('#tpl-customer-tel').html());
            renderSetLegalTel();
            renderDelTel();
        };

        var tableDone = function(res, curr, count) {
            if (res.items && res.items.length > 0) {
                var items = res.items;
                $('div[lay-id="customerId"]').find('a[lay-event="lig-detail"]').mouseover(function() {
                    var index = $(this).closest('tr').attr('data-index'), it = items[index], id = it.id;
                    lig.ajaxget({
                        url: 'bu/customer/' + id,
                        success: function(result) {
                            laytpl($('#tpl-customer-detail').html()).render(result.data, function (html) {
                                layer.open({
                                    type: 1,
                                    title: false,
                                    content: html,
                                    closeBtn: 0,
                                    area: ['40%', '100%'],
                                    offset: 'auto',
                                    shadeClose: true,
                                    success: function(layero){
                                        layero.css({'left': 'auto','right': '0'});
                                        element.render('collapse');
                                    }
                                });
                            });
                        }
                    });
                });
            }
        }


        var customerTable;
        var renderTable = function() {
            customerTable = lig.table({
                elem: '[lay-filter="lig-customer-table"]',
                url: 'bu/customer/page',
                cols: [[
                    { title: '', type: 'checkbox', fixed: true, width: 60 },
                    { title: '操作', toolbar: '#tpl-customer-table-row-toolBar', width: 120},
                    { title: '公司', field: 'name', minWidth: 160 },
                    { title: '法人', field: 'legal', width: 100 },
                    { title: '状态', field: 'statusName', width: 100 },
                    { title: '微信', field: 'wxNumber', width: 60, align: 'center'},
                    { title: '业务员', field: 'salesName', width: 100 },
                    { title: '行业', field: 'industry', width: 140 },
                    { title: '待跟进时间', field: 'statusTime', minWidth: 100, templet: '<div>{{ layui.lig.toDateString(d.followTime, "yyyy-MM-dd") }}</div>', },
                    { title: '状态更改时间', field: 'statusTime', minWidth: 120, templet: '<div>{{ layui.util.toDateString(d.statusTime, "yyyy-MM-dd HH:mm:ss") }}</div>', }
                ]],
                done: function (res, curr, count) {
                    tableDone(res, curr, count);
                }
            },{
                mode:'dialog',
                entity: 'customer',
                primaryField: 'id',
                addContent: $('#tpl-customer-add').html(),
                addUrl: 'bu/customer/add',
                addTitle: '添加客户',
                area: ['50%', 'auto'],
                onAddOpened: function() {
                    var _saveForm = '[lay-filter="lig-saveForm"]';
                    lig.loadSelectDict('bu_customer_status', _saveForm + ' select[name="status"]');

                    $('[lay-filter="lig-customer-tel-add"]').click(function () {
                        renderAddTel();
                    });
                    renderSetLegalTel();
                    renderCancelLegalTel();
                    renderWaitTel();
                },

                modifyContent: $('#tpl-customer-modify').html(),
                modifyUrl: 'bu/customer/modify',
                modifyTitle: '编辑客户信息',
                onModifyOpened: function() {
                    var _saveForm = '[lay-filter="lig-saveForm"]';
                    lig.loadSelectDict('bu_customer_status', _saveForm + ' select[name="status"]', true);

                    $('[lay-filter="lig-customer-tel-add"]').click(function () {
                        renderAddTel();
                    });
                    renderSetLegalTel();
                    renderCancelLegalTel();
                    renderWaitTel();

                    initStatus = $(_saveForm + ' select[name="status"]').attr('lay-data');

                    laydate.render({
                        elem: _saveForm + ' input[name="followTime"]'
                    });

                    form.on('checkbox(lig-notFollow)', function(data){

                      if (data.elem.checked) {
                        $(_saveForm + ' input[name="followTime"]').val('');
                        $(_saveForm + ' input[name="followTime"]').attr('disabled', true);
                        $(_saveForm + ' input[name="followTime"]').addClass('layui-disabled');
                      } else {
                        $(_saveForm + ' input[name="followTime"]').attr('disabled', false);
                        $(_saveForm + ' input[name="followTime"]').removeClass('layui-disabled');
                      }
                    }); 
                },

                onSave: function(data) {
                    var tels = [], isSetLegal = false;
                    $('.customer-tels > tr').each(function () {
                        var $this = $(this);
                        var telNumber = $this.find('input[name="telNumber"]').val();
                        if (telNumber == '') {
                            return true;
                        }
                        var isLegal = $this.find('input[name="isLegal"]').val();
                        if (!isSetLegal) {
                            isSetLegal = isLegal == '1' ? true : false;
                        }
                        tels.push({telNumber: telNumber, isLegal: isLegal, isInvalid: $this.find('input[name="isInvalid"]').val()});
                    });

                    if (data.status == '01001' && !isSetLegal) {
                        lig.alert('请设置法人电话');
                        return false;
                    }

                    data.tels = JSON.stringify(tels);
                    return data;
                },

                detailContent: $('#tpl-customer-detail').html(),
                onDetailOpened: function() {
                    element.render('collapse');
                },
                removeUrl: 'bu/customer/remove',
                getUrl: 'bu/customer/{id}'
            });
        }

        var checkData = function(checkType) {
            var ids = getSelected();
            if (ids == '') {
                lig.alert('请选择数据');
                return;
            }

            lig.ajaxget({
                url: 'bu/customer/checkData',
                data: {
                    checkType: checkType,
                    customerIds: ids
                },
                success: function(res) {
                    if (res.data) {
                        var msg = '';
                        if (checkType == 1) {
                            msg = '完成空号检测，共' + res.data.total + '条，空号' + res.data.dealTotal + '条';
                        } else if (checkType == 3) {
                            msg = '完成二要素检测，共' + res.data.total + '条，一致' + res.data.dealTotal + '条';
                        }
                        lig.alert(msg);
                    }
                }
            });
        }

        var toSalesByChoose = function() {
            var ids = getSelected();
            if (ids == '') {
                lig.alert('请选择数据');
                return;
            }

            // 弹出用户选择页面
            lig.choose({
                title: '选择业务员',
                url: 'bu/chooseSales',
                onSave: function () {
                },
                end: function () {
                    if (choose_user) {
                        lig.ajaxpost({
                            url: 'bu/customer/toSales',
                            data: {customerIdList: ids, salesId: choose_user.userId},
                            success: function(result) {
                                lig.success('分配成功');
                                customerTable.reloadTable({done: tableDone});
                            }
                        });
                    }
                }
            });
        }

        var toSalesByQuantity = function() {
            salesQuantity_dataJson = '';
            //console.log('2222');
            //admin.navigate('bu/salesQuantity');
            // 弹出用户选择页面
            lig.choose({
                title: '按数量分配客户',
                url: 'bu/salesQuantity',
                height: '90%',
                width: '80%',
                onSave: function () {
                },
                end: function () {
                    if (salesQuantity_dataJson) {
                        lig.ajaxpost({
                            url: 'bu/customer/toSalesByQuantity',
                            data: {dataJson:JSON.stringify(salesQuantity_dataJson)},
                            contentType: "application/json;charset=UTF-8",
                            success: function(res) {
                                lig.success("分配成功");
                                customerTable.reloadTable();
                            }
                        });
                    }
                }
            });
        }


        var _searchForm = '[lay-filter="lig-customer-searchform"]';
        laydate.render({
            elem: _searchForm + ' input[name="startTime"]'
        });
        laydate.render({
            elem: _searchForm + ' input[name="endTime"]'
        });
        lig.loadSelectDict('bu_customer_time_tag', _searchForm + ' select[name="timeTag"]');
        form.render();

        lig.ajaxget({
            url: 'setting/dict/list/constant/bu_customer_status',
            success: function (result) {
                if (result.data) {
                    var defStatus = '';
                    $.each(result.data, function (i, _it) {
                        var thisClass = '';    
                        if (i == 0) {
                            thisClass = 'class="layui-this"';
                            defStatus = _it.dictCode;
                        }
                        $('#customer_status').append('<li ' + thisClass + ' data-status="' + _it.dictCode + '">' + _it.dictName + '</li>');
                    });

                    $('[lay-filter="lig-customer-searchform"] input[name="status"]').val(defStatus);
                    renderTable();
                }
            }
        });

        lig.ajaxget({
            url: 'bu/sales/user/list',
            success: function(result) {
                var $salesIdSelect = $('[lay-filter="lig-customer-searchform"] select[name="salesId"]');

                $.each(result.data, function(i, it) {
                    $salesIdSelect.append('<option value="' + it.userId + '">' + it.userName + '</option>');
                });
                form.render();
            }
        });

        element.on('tab(lig-customer-status-tab)', function(data){
            $('[lay-filter="lig-customer-searchform"] input[name="status"]').val($(this).attr('data-status'));
            customerTable.reloadTable({done: tableDone});
        });

        

        $('[lay-filter="lig-customer-import"]').click(function() {
            lig.importData({
                tplCode: 'customer_data_import_tpl',
                importUrl: 'bu/customer/excel/import',
                end: function() {
                    customerTable.reloadTable({done: tableDone});
                }
            });
        });

        $('[lay-filter="lig-customer-export"]').click(function() {
            lig.export('[lay-filter="lig-customer-searchform"]', 'bu/customer/excel');
        });

        $('[lay-filter="lig-customer-moveStatus"]').click(function(){
            var ids = getSelected();
            if (ids == '') {
                lig.alert('请选择数据');
                return;
            }

            lig.dialog({
                title: '更改状态',
                area: ['40%', '200px'],
                saveUrl: 'bu/customer/moveStatus',
                content: $('#tpl-customer-moveStatus').html(),
                onOpened: function() {
                    var _saveForm = '[lay-filter="lig-saveForm"]';
                    lig.loadSelectDict('bu_customer_status', _saveForm + ' select[name="status"]', true);
                    $('.layui-layer-content').css('overflow','visible');
                },
                onSave: function(data) {
                    data.customerIds = ids;
                    return data;
                },
                onCompleted: function() {
                    customerTable.reloadTable({done: tableDone});
                }
            });
        });


        layui.dropdown1.render({
            elem: '[lay-filter="lig-customer-toSales"]',
            click: function(name) {
                if (name == '1') {
                    toSalesByChoose();
                } else if (name  == '2') {
                    toSalesByQuantity();
                }
            },
            options: [
              {
                name: '1',
                title: '按选择分配'
              },
              {
                name: '2',
                title: '按数量分配'
              }
            ]
        });

        var catData = [{"id":1,"name":"周边旅游","children":[{"id":24,"name":"广东","status":0,"children":[{"id":7,"name":"广州"},{"id":23,"name":"潮州"}]}]},{"id":5,"name":"国内旅游","children":[{"id":8,"name":"华北地区","children":[{"id":9,"name":"北京"}]}]},{"id":6,"name":"出境旅游","children":[{"id":10,"name":"东南亚","children":[{"id":11,"name":"马来西亚","children":[{"id":20,"name":"沙巴","children":[{"id":21,"name":"美人鱼岛","children":[{"id":22,"name":"潜水"}]}]}]}]}]}];
        //无限级分类-所有配置
        var catIns2 = selectN({
          //元素容器【必填】
          elem: '#cat_ids2'
          //候选数据【必填】
          ,data: catData 
          //设置了长度
          ,width:null
          //默认值
          ,selected: [6,10,11] 
          
          //为真只取最后一个值
          ,last:true       
  
            //空值项提示，可设置为数组['请选择省','请选择市','请选择县']
            ,tips: '请选择'

            //事件过滤器，lay-filter名 不设置与选择器相同(去#.)
            ,filter: ''

            //input的name 不设置与选择器相同(去#.)
            ,name: 'cat2'
            
            //数据分隔符
            ,delimiter: ','
            
            //数据的键名
            ,field:{idName:'id',titleName:'name',childName:'children'}
            
            //表单区分 form.render(type, filter); 为class="layui-form" 所在元素的 lay-filter="" 的值 
            ,formFilter: null      
      
        }); 

    })
</script>